<?php

/**
 * 帮助中心详情 控制器
 * 
 * @author zhangbo <zhyangbo@shopexunit.com>
 */
class content_ctl_admin_helps_detail extends content_admin_controller {

    public $platforms = array('pc' => '电脑端', 'wap' => '移动端');

    /**
     * editor
     * 
     * @param int $type
     */
    public function _editor($type = 1) {
        switch ($type) {
            case 1:
                $sections = array(
                    'basic' => array(
                        'label' => app::get('content')->_('基本信息'),
                        'options' => '',
                        'file' => 'admin/helps/detail/basic.html',
                    ),
                    'ext' => array(
                        'label' => app::get('content')->_('扩展属性'),
                        'options' => '',
                        'file' => 'admin/helps/detail/ext.html',
                    ),
                    'articleimage' => array(
                        'label' => app::get('content')->_('文章配图'),
                        'options' => '',
                        'file' => 'admin/helps/detail/articleimage.html',
                    ),
                    'body' => array(
                        'label' => app::get('content')->_('文章内容'),
                        'options' => '',
                        'file' => 'admin/helps/detail/body.html',
                    ),
                );
                break;
            case 2:
                $sections = array(
                    'basic' => array(
                        'label' => app::get('content')->_('基本信息'),
                        'options' => '',
                        'file' => 'admin/helps/detail/basic.html',
                    ),
                    'seo' => array(
                        'label' => app::get('content')->_('SEO设置'),
                        'options' => '',
                        'file' => 'admin/helps/detail/seo.html',
                    ),
                    'single' => array(
                        'label' => app::get('content')->_('可视化编辑'),
                        'options' => '',
                        'file' => 'admin/helps/detail/single.html',
                    ),
                );
                break;
            case 3:
                $sections = array(
                    'basic' => array(
                        'label' => app::get('content')->_('基本信息'),
                        'options' => '',
                        'file' => 'admin/helps/detail/basic.html',
                    ),
                    'seo' => array(
                        'label' => app::get('content')->_('SEO设置'),
                        'options' => '',
                        'file' => 'admin/helps/detail/seo.html',
                    ),
                    'custom' => array(
                        'label' => app::get('content')->_('自定义内容'),
                        'options' => '',
                        'file' => 'admin/helps/detail/custom.html',
                    ),
                );
                break;
            default:
        }

        foreach (kernel::serviceList('content_helps_detail_sections') as $object) {
            if (!method_exists($object, 'get_section')) {
                continue;
            }
            $sections = array_merge($sections, $object->get_section());

            //设置页面信息 
            if (!method_exists($object, 'set_page_data')) {
                continue;
            }
            $object->set_page_data($this);
        }

        $this->pagedata['sections'] = $sections;
    }

    /**
     * 添加帮助信息
     */
    public function add() {
        $this->_editor(1);
        header("Cache-Control:no-store");
        $this->singlepage('admin/helps/detail/editor.html');
    }

    /**
     * 编辑帮助信息
     */
    public function edit() {
        $this->begin('index.php?app=content&ctl=admin_helps');
        $article_id = $this->_request->get_get('article_id');
        $helps['indexs'] = app::get('content')->model('article_helps')->dump($article_id, '*');
        if (empty($helps['indexs'])) {
            $this->end(false, app::get('content')->_('错误请求'));
        }
        $this->_editor(1);
        $hot_link = $helps['indexs']['hot_link'];
        if (is_array($hot_link)) {
            foreach ($hot_link['linkwords'] AS $key => $val) {
                $helps['ext']['hot'][$key]['linkwords'] = $val;
                $helps['ext']['hot'][$key]['linkurl'] = $hot_link['linkurl'][$key];
            }
        }
        $this->pagedata['helps'] = $helps;
        $this->pagedata['platform_options'] = $this->platforms;
        header("Cache-Control:no-store");

        $this->singlepage('admin/helps/detail/editor.html');
    }

    /**
     * 保存帮助信息
     */
    public function save() {
        $this->begin();
        $dtime = $this->_request->get_post('_DTIME_');
        $post = $this->_request->get_post('helps');
        $article_id = $this->_request->get_post('article_id');
        if (!empty($post['indexs']['pubtime'])) {
            $post['indexs']['pubtime'] = $post['indexs']['pubtime'] . ' ' . $dtime['H']['article[indexs']['pubtime'] . ':' . $dtime['M']['article[indexs']['pubtime'];
            $post['indexs']['pubtime'] = strtotime($post['indexs']['pubtime']);
        }
        if ($post['ext']['enable_goods_info'] > 0) {
            $post['indexs']['goods_info'] = $post['ext']['goods'];
        } else {
            $post['indexs']['goods_info'] = '';
        }
        if ($post['ext']['enable_hot_link'] > 0) {
            $post['indexs']['hot_link'] = $post['ext']['hot'];
        } else {
            $post['indexs']['hot_link'] = '';
        }
        if ($article_id > 0) {
            $res = app::get('content')->model('article_helps')->update($post['indexs'], array('article_id' => $article_id));
            if ($res) {
                $this->end(true, app::get('content')->_('保存成功'));
            } else {
                $this->end(false, app::get('content')->_('保存失败'));
            }
        } else {
            $res = app::get('content')->model('article_helps')->insert($post['indexs']);
            if ($res) {
                $post['helps']['article_id'] = $res;
                $this->end(true, app::get('content')->_('添加成功'), null, array('id' => $post['helps']['article_id']));
            } else {
                $this->end(false, app::get('content')->_('添加失败'));
            }
        }
    }

}
